package com.net.xpay.admin.controller.admin;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.xpay.admin.vo.OperatorUserVo;
import com.net.xpay.common.domain.operator.OperatorUser;
import com.net.xpay.common.enums.user.SecurityStatus;
import com.net.xpay.common.manager.operator.OperatorUserBindingManager;
import com.net.xpay.common.manager.operator.OperatorUserManager;
import com.net.xpay.core.model.IdModel;
import com.net.xpay.core.model.OperatorUsersSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/operation-user")
@Api(tags = "运营/多商户管理", description = "运营 多商户管理 qyc")
public class AdminOperatorUserController {

    @Autowired
    private OperatorUserManager operatorUserManager;

    @Autowired
    private OperatorUserBindingManager operatorUserBindingManager;


    @ApiOperation("搜索")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'operation-user::search')")
    public ResultPagedList<OperatorUserVo> searchOperatorUser(@RequestBody OperatorUsersSearchModel searchModel) {
        List<OperatorUser> records = operatorUserManager.search(searchModel);
        long count = operatorUserManager.searchCount(searchModel);
        List<OperatorUserVo> vos = BeanCopierUtil.batchCopy(OperatorUser.class, OperatorUserVo.class,records);
        for (OperatorUserVo vo : vos) {
            Long companyCount = operatorUserBindingManager.byOperatorUserIdCount(vo.getId());
            vo.setCompanyCount(companyCount);
        }

        return new ResultPagedList<>(vos, count, searchModel);
    }

    @ApiOperation("修改操盘手状态")
    @PostMapping("/update-status")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'operation-user::update-status')")
    public ResultData<Boolean> updateOperatorUserSecurityStatus(@RequestBody IdModel model) {
        Preconditions.checkArgument(model.getId() != null);
        OperatorUser operatorUser = operatorUserManager.getById(model.getId());
        operatorUser.setSecurityStatus(SecurityStatus.FROZEN);
        operatorUserManager.updateByPrimaryKeyWithVersion(operatorUser);
        return new ResultData<>(true);
    }

    @ApiOperation("修改操盘手密码")
    @PostMapping("/reset-password")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'operation-user::reset-password')")
    public ResultData<String> resetOperatorUserPassword(@RequestBody IdModel model) {
        Preconditions.checkArgument(model.getId() != null);
        OperatorUser operatorUser = operatorUserManager.getById(model.getId());
        String psw = RandomStringUtils.randomAlphabetic(3).toLowerCase() + "@" + Strings.padStart("" + RandomUtils.nextInt(0, 1000000), 6, '0');
        operatorUser.setPassword(BCrypt.hashpw(psw, BCrypt.gensalt()));
        operatorUserManager.updateByPrimaryKeyWithVersion(operatorUser);
        return new ResultData<>(psw);
    }
}
